// @ts-nocheck
import {createRouter, createWebHashHistory, RouteRecordRaw} from 'vue-router'
import Login from '@/views/login/LoginView.vue'
import YwuRouteIndex from '@/views/routeIndex/YwuRouteIndex'
import Layout from '@/layout/index'
import store from '@/store'
import {registerComponent} from "@/utils/tagCreate";
import {queryMenuRegisterInfo} from "@/request/menu";

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    component: YwuRouteIndex,
    name: 'ywu-route-index'
  },
  {
    path: '/ywu',
    component: Layout,
    name: 'ywu-home',
    redirect: '/ywu/index',
    meta: { title: '首页' },
    children: []
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/error/404.vue')
  },
  {
    path: '/403',
    name: '403',
    component: () => import('@/views/error/403.vue')
  },
  {
    path: '/400',
    name: '400',
    component: () => import('@/views/error/400.vue')
  },
  {
    path: '/500',
    name: '500',
    component: () => import('@/views/error/500.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

async function doRouterBeforeEach(to, from, next) {
  if (!judgeRouteIsExist(to.path)) {
    // 查询菜单、文根、组件等相关信息
    let requestParam = {
      routeTo: to.fullPath
    }
    let registerInfo;
    try {
      registerInfo = await queryMenuRegisterInfo(requestParam);
    } catch (e) {
      await router.replace({path: "/404"})
      return
    }
    // 组件信息
    let componentInfoList = registerInfo.data.componentInfo;
    // 文根信息
    let routeRootPathList = registerInfo.data.routeRootPath;
    // 菜单信息
    let menuInfo = registerInfo.data.menuInfo;
    // 如果查询出的菜单结果信息为空，则进行404
    if (!menuInfo || !menuInfo.id) {
      await router.push({path: "/404"})
    }
    // 注入所有的组件
    for (let i = 0; i < componentInfoList.length; i++) {
      let componentInfo = componentInfoList[i];
      registerComponent(componentInfo);
    }
    // 路由文根关联路由组件并注入到VUE-ROUTER中
    for (let i = 0; i < routeRootPathList.length; i++) {
      let data = routeRootPathList[i]
      if (data.routeRootPath === '/ywu') {
        continue;
      }
      // 路由文根不存在，创建路由文根路由对象
      if (!judgeRouteIsExist(data.routeRootPath)) {
        let componentFilterList = componentInfoList.filter(res => res.id === data.componentId)
        if (componentFilterList.length === 0) {
          continue;
        }
        let componentFilter = componentFilterList[0]
        let routeInfo = {
          path: data.routeRootPath,
          name: data.routeRootName,
          component: store.state.app.component(componentFilter.componentName)
        }
        router.addRoute(routeInfo);
      }
    }
    // 根据查询出的数据构建VUE-ROUTER
    handlerRoute(menuInfo, "", componentInfoList, false, routeRootPathList)
    // 再次判断路由是否存在
    if (!judgeRouteIsExist(to.path)) {
      await router.replace({path: "/404"})
    } else {
      next(to);
    }
  } else {
    next()
  }
}

// 前置路由守卫  -  获取后端存储的动态路由
router.beforeEach((to, from, next) => {
  doRouterBeforeEach(to, from, next).then(r => {})
})

// 递归创建路由 -- 子路由
function handlerRoute(data, parentName, componentRes, isChild, routeRootPathList) {
  // 判断路由是否存在
  if (judgeRouteIsExist(data.routeTo)) {
    // 判断当前路由是否存在，存在则递归进行子路由的创建
    if (data.children !== null && data.children.length > 0) {
      handlerRoute(data.children[0], data.name, componentRes, true, routeRootPathList);
    }
    return;
  }
  // 添加当前路由
  let resDataInfo = {}
  // 判断当前菜单类型是否为动态组件
  if (data.menuType === 'DYNAMIC') {
    // 动态组件
    // 根据componentId关联组件
    let componentInfoList = componentRes.filter(res => res.id === data.componentId);
    if (componentInfoList && componentInfoList.length > 0) {
      let componentInfo = componentInfoList[0];
      // 构建路由组件
      let newMeta = data.meta.replaceAll("\'", "\"")
      resDataInfo = {
        path: `${data.routePath}`,
        component: store.state.app.component(componentInfo.componentName),
        name: `${data.name}`,
        meta: JSON.parse(newMeta)
      };
    }
  } else {
    // 静态菜单
    let newMeta = data.meta.replaceAll("\'", "\"")
    resDataInfo = {
      path: `${data.routePath}`,
      component: () => import(`@/views/${data.componentPath}`),
      name: `${data.name}`,
      meta: JSON.parse(newMeta)
    };
  }
  if (parentName === "") {
    let routeRootFilterList = routeRootPathList.filter(res => res.id === data.routeRootId);
    if (routeRootFilterList.length === 0) {
      console.log("route root path error")
      return
    }
    let routeRootFilter = routeRootFilterList[0]
    parentName = routeRootFilter.routeRootName;
  }
  router.addRoute(parentName, resDataInfo);
  // 判断当前路由是否存在，存在则递归进行子路由的创建
  if (data.children && data.children.length > 0) {
    handlerRoute(data.children[0], data.name, componentRes, true, routeRootPathList);
  }
}

// 判断当前执行的路由是否存在
function judgeRouteIsExist(path) {
  const routesInfo = router.getRoutes();
  const tempList = routesInfo.filter(res => res.path === path);
  return tempList.length > 0;
}

export default router
